<?php
date_default_timezone_set("America/Lima");
$fecha = date("Y/m/d");
$hh = date("H");
if ($hh < 12)
    $mer = "am";
else
    $mer = "pm";
$hora = date("h:i:s") . " $mer";
?>
<script>
    $(function() {
        var url;
        $("#nombre").keyup(function() {
            if ($("#nombre").val().length > 2) {
                url = '<?php echo base_url("intranet/reserva/clientesRegistrados"); ?>/' + $("#nombre").val();
            } else {
                url = '<?php echo base_url("intranet/reserva/clientesRegistrados/vacio"); ?>';
            }
            filtrarRegistros(url);
        });
        ventana("productos", 500, 500, true, "center");
        ventana("detalleHabitacion", 320, 520, true, "center");
    });
</script>
<div class="page-content">
    <div class="page-header position-relative">
        <h1>Clientes Registrados<small><i class="icon-double-angle-right"></i> Listado</small></h1>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <div class="widget-box">
                <div class="widget-header"><h4>Lista de Clientes</h4>
                    <span class="widget-toolbar">
                        <a data-action="collapse" href="#">
                            <i class="icon-chevron-up"></i>
                        </a>
                    </span>
                </div>
                <div class="widget-body">
                    <div class="widget-body-inner">
                        <div class="widget-main">
                            <div class="dataTables_wrapper" role="grid">
                                <div class="row-fluid">
                                    <div class="span6"></div>
                                    <div class="span6">
                                        <div class="dataTables_filter">
                                            <label>Buscar <input type="text" id="nombre" name="nombre"></label>
                                        </div>
                                    </div>
                                </div>
                                <div>
                                    <table id="registros" class="table table-striped table-bordered table-condensed table-hover"> 
                                        <thead>
                                            <tr><th>Cliente</th>
                                                <th>DNI</th>
                                                <th>Habitación</th>
                                                <th>Fecha Ingreso</th>
                                                <th>Personal</th>
                                                <th>Detalle</th>
                                            </tr>
                                        </thead>
                                        <tbody id="datosRegistro">
                                            <?php
                                            //if (!empty($registros)) :
                                            if (isset($registros)) :
                                                foreach ($registros as $fila):
                                                    ?>
                                                    <tr>
                                                        <td style="cursor: pointer;" onclick="DownUp(<?php echo $fila->idRegistro ?>)"><?php echo $fila->nombre; ?></td>
                                                        <td><?php echo $fila->dni; ?></td>
                                                        <td><?php echo $fila->nro_habi; ?></td>
                                                        <td><?php echo $fila->fec_ing; ?></td>
                                                        <td><?php echo $fila->nom_per; ?></td>
                                                        <td class="td-actions">
                                                            <div class="hidden-phone visible-desktop action-buttons">
                                                                <a class="blue" href="#"><i class="icon-list-alt bigger-150" title="Detalle" onclick="DownUp(<?php echo $fila->idRegistro ?>)"></i></a>
                                                                <a class="green" href="#"><i class=" icon-exclamation-sign bigger-150" title="Información adicional" onclick="ventRegistroInf(<?php echo $fila->idRegistro ?>)"></i></a>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr id="<?php echo $fila->idRegistro ?>" class="detalle" style="display: none;">
                                                        <td colspan="6">
                                                            <div id="det<?php echo $fila->idRegistro ?>" style="background-color: #ebebeb; border: 3px solid #cfcfcf; padding: 10px;">
                                                                <legend>Consumo productos</legend>
                                                                <div class="row-fluid">
                                                                    <div class="span12">                                                                
                                                                        <div class="widget-box collapsed">
                                                                            <div class="widget-header widget-header-small header-color-orange" style="cursor: pointer;" onclick="desplegar(<?php echo $fila->idRegistro ?>);">
                                                                                <h6>
                                                                                    <i class="icon-sort"></i>
                                                                                    Agregar Producto
                                                                                </h6>
                                                                                <span class="right-align">Cliente: <?php echo $fila->nombre; ?></span>
                                                                                <div class="widget-toolbar">
                                                                                    <a href="#">
                                                                                        <i class="icon-chevron-down"></i>
                                                                                    </a>
                                                                                </div>
                                                                            </div>

                                                                            <div class="widget-body" id="cuerpo<?php echo $fila->idRegistro ?>">
                                                                                <div class="widget-main">
                                                                                    <form action="#" class="form-horizontal">
                                                                                        <div class="row-fluid">
                                                                                            <div class="span6">
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="nom">Producto</label>
                                                                                                    <div class="controls">
                                                                                                        <input type='hidden' id='cod<?php echo $fila->idRegistro; ?>' value="">
                                                                                                        <input type="text"  id="desc<?php echo $fila->idRegistro; ?>">
                                                                                                        <button class="blue btn btn-small btn-primary" onclick="abrirProductos(<?php echo $fila->idRegistro; ?>)" type="button">
                                                                                                            <i class="icon-search"></i>
                                                                                                        </button>
                                                                                                    </div>                                            
                                                                                                </div>
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="prec<?php echo $fila->idRegistro; ?>">Precio</label>
                                                                                                    <div class="controls">
                                                                                                        <input type='text' id='prec<?php echo $fila->idRegistro; ?>' class="span3" value="" >
                                                                                                    </div>                                            
                                                                                                </div>

                                                                                            </div>
                                                                                            <div class="span6">
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="canti<?php echo $fila->idRegistro; ?>">Cantidad</label>
                                                                                                    <div class="controls">
                                                                                                        <input type='text' id='canti<?php echo $fila->idRegistro; ?>' value="0" oninput="calcsubtotal(<?php echo $fila->idRegistro; ?>)" class="span3">
                                                                                                    </div>                                            
                                                                                                </div>
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="subTotal<?php echo $fila->idRegistro; ?>">Sub Total</label>
                                                                                                    <div class="controls">
                                                                                                        <input type='text' id='subTotal<?php echo $fila->idRegistro; ?>' value="0.00" maxlength='8' class="span3">
                                                                                                    </div>                                            
                                                                                                </div>
                                                                                            </div>
                                                                                            <div class="span11">
                                                                                                <div class="form-actions">
                                                                                                    <div class="width-100"></div>
                                                                                                    <button class="btn btn-info btnregistrar" onclick="agregar(<?php echo $fila->idRegistro; ?>)" type="button">
                                                                                                        <i class="icon-ok bigger-110"></i>
                                                                                                        Agregar
                                                                                                    </button>
                                                                                                    &nbsp; &nbsp; &nbsp;
                                                                                                    <button class="btn" type="reset">
                                                                                                        <i class="icon-undo bigger-110"></i>
                                                                                                        Limpiar
                                                                                                    </button>
                                                                                                </div>
                                                                                            </div>
                                                                                        </div>
                                                                                        <!--<div class="row-fluid">
                                                                                            
                                                                                        </div> -->
                                                                                    </form>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                </div>                                                                
                                                                <div class="row-fluid">
                                                                    <div class="span12">
                                                                        <div class="widget-box">
                                                                            <div class="widget-header widget-header-small header-color-orange">
                                                                                <h5 class="bigger lighter">
                                                                                    <i class="icon-table"></i>
                                                                                    Detalle Registro
                                                                                </h5>
                                                                            </div>

                                                                            <div class="widget-body">
                                                                                <div class="widget-main no-padding">
                                                                                    <table id="tabla<?php echo $fila->idRegistro; ?>" class="table table-striped table-bordered">
                                                                                        <thead>
                                                                                            <tr>
                                                                                                <th style="display: none;">Código</th>
                                                                                                <th>Producto</th>
                                                                                                <th>Precio</th>
                                                                                                <th>Cantidad</th>
                                                                                                <th>Sub Total</th>
                                                                                                <th>Fecha</th>
                                                                                                <th>Acciones</th>
                                                                                            </tr>
                                                                                        </thead>
                                                                                        <tbody>
                                                                                        </tbody>
                                                                                    </table>
                                                                                    <div style="float: right; margin-top: 5px; color: blue; font-weight: bold">
                                                                                    <!--    Total: <input type="text" class="left total" disabled="disabled"> -->
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                    <!--<div>
                                                                    </div> -->
                                                                </div>
                                                                <div class="space-10"></div>
                                                                <legend>Cierre de Alquiler de Habitación</legend>
                                                                <div class="" style="overflow: hidden">
                                                                    <div class="span12">
                                                                        <div class="widget-box">
                                                                            <div class="widget-header widget-header-small header-color-orange">
                                                                                <h5 class="bigger lighter">
                                                                                    <i class="icon-table"></i>
                                                                                    Montos
                                                                                </h5>
                                                                            </div>

                                                                            <div class="widget-body">
                                                                                <div class="widget-main">
                                                                                    <form action="<?php echo base_url("intranet/reserva/cerrarRegistro/$fila->idRegistro") ?>" class="form-horizontal">
                                                                                        <div class="row" style="margin-left: 20px">
                                                                                            <div class="span6">
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="totalProd<?php echo $fila->idRegistro; ?>">Consumo Productos</label>
                                                                                                    <div class="controls">
                                                                                                        <div class="input-prepend">
                                                                                                            <span class='add-on'>
                                                                                                                <i class='icon-dollar'></i>
                                                                                                            </span>
                                                                                                            <input type="text"  id="totalProd<?php echo $fila->idRegistro; ?>" value="0" class="span5 total">
                                                                                                        </div>
                                                                                                    </div>                                            
                                                                                                </div>
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="totalHabit<?php echo $fila->idRegistro; ?>">Consumo Habitación</label>
                                                                                                    <div class="controls">
                                                                                                        <div class="input-prepend">
                                                                                                            <span class='add-on'>
                                                                                                                <i class='icon-dollar'></i>
                                                                                                            </span>
                                                                                                            <input type='text' id='totalHabit<?php echo $fila->idRegistro; ?>' value="0" class="span5">                                                                                                            
                                                                                                        </div>
                                                                                                        <span style="margin-left: -88px">
                                                                                                            <button class="blue btn btn-small btn-primary" title="Monto por días" onclick="abrirDetHab(<?php echo $fila->idRegistro; ?>);" type="button">
                                                                                                                <i class="icon-eye-open"></i>
                                                                                                            </button></span>
                                                                                                    </div>                                            
                                                                                                </div>
                                                                                                <hr>
                                                                                                <div class="control-group">
                                                                                                    <label class="control-label" for="montoTotal<?php echo $fila->idRegistro; ?>">Monto Total</label>
                                                                                                    <div class="controls">
                                                                                                        <div class="input-prepend">
                                                                                                            <span class='add-on'>
                                                                                                                <i class='icon-dollar'></i>
                                                                                                            </span>
                                                                                                            <input type='text' value="0" id='montoTotal<?php echo $fila->idRegistro; ?>' class="span5" >
                                                                                                        </div>
                                                                                                        <span style="margin-left: -88px">
                                                                                                            <button class="blue btn btn-small btn-primary" onclick="calcMontoTotal(<?php echo $fila->idRegistro; ?>)" type="button" title="Calcular">
                                                                                                                <i class="icon-cog"></i>
                                                                                                            </button>
                                                                                                        </span>
                                                                                                    </div>
                                                                                                </div>
                                                                                            </div>
                                                                                            <div class="span11">
                                                                                                <div class="form-actions">
                                                                                                    <div class="width-100"></div>
                                                                                                    <button class="btn btn-info" type="submit">
                                                                                                        <i class="icon-ok bigger-110"></i>
                                                                                                        Finalizar
                                                                                                    </button>
                                                                                                </div>
                                                                                            </div>
                                                                                        </div>
                                                                                    </form>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    </div> 
                                                                </div>
                                                            </div>
                                                        </td>
                                                    </tr>                                      
                                                    <?php
                                                endforeach;
                                            endif;
                                            ?>   
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="productos">
    <div class="widget-box">
        <div class="widget-header header-color-orange">
            <h5 class="bigger lighter">
                <i class="icon-table"></i>
                Productos
            </h5>
        </div>

        <div class="widget-body">
            <div class="widget-main no-padding" id="tblProd">
            </div>
        </div>
    </div>
</div>
<div id="detalleHabitacion" title="Monto por días">
    <div class="row-fluid">
        <div class="span7">
            <form style="padding-left: 20px" id="subMontoAlq">
                <input type="hidden" id="numMontos">
                <input type="hidden" id="idTH">
            </form>
        </div>
        <div class="span5">
            <div style="margin-top: 90px">
                <div style="position: fixed">
                    <button class="btn btn-app btn-primary" id="nuevoElem">
                        <i class="icon-plus bigger-230"></i>
                        Agregar 
                    </button>
                    <div class="space-10"></div>
                    <button class="btn btn-app btn-success" id="totalizar">
                        <i class="icon-refresh bigger-230"></i>
                        Totalizar 
                    </button>
                </div>
            </div>   
        </div>
    </div>
</div> 
<div id="infoRegistro"></div>
<script type="text/javascript">
    function DownUp(id) {
        if ($("#" + id).css("display") === "none") {
            $("#det" + id).slideDown();
            $(".total").val("");
            detalleRegistro(id, "<?php echo base_url('intranet/reserva/detalleRegistro') ?>/" + id);
            $("#" + id).css("display", "table-row");
        } else {
            if ($("#" + id).css("display") === "table-row") {
                $("#" + id).css("display", "none");
            }
        }
    }

    function calcsubtotal(id) {
        $("#subTotal" + id).val($("#prec" + id).val() * $("#canti" + id).val());
    }

    function agregar(id) {
        if ($("#cod" + id).val() !== "") {
            $.ajax({
                type: "POST",
                url: '<?php echo base_url("intranet/reserva/registrarDetalleReg"); ?>',
                data: {registro: id,
                    producto: $("#cod" + id).val(),
                    cantidad: $("#canti" + id).val(),
                    subTotal: $("#subTotal" + id).val()},
                //contentType: "application/json",
                dataType: "json",
                success: function(cod) {
                    if (cod !== null) {
                        $(".total").val(parseFloat($(".total").val() === "" ? 0 : $(".total").val()) + parseFloat($("#subTotal" + id).val()));
                        $("#tabla" + id + " tbody").append("<tr><td style='display:none'>" + $("#cod" + id).val() + "</td>\n\
                                                                                      <td>" + $("#desc" + id).val() + "</td>\n\
                                                                                      <td>" + $("#prec" + id).val() + "</td>\n\
                                                                                      <td>" + $("#canti" + id).val() + "</td>\n\
                                                                                      <td>" + $("#subTotal" + id).val() + "</td>\n\\n\
                                                                                      <td>" + "<?php echo $fecha . " " . $hora ?>" + "</td>\n\
                                                                                      <td class='td-actions'>" +
                                "<div class='hidden-phone visible-desktop action-buttons'>" +
                                "<a class='red' onclick='eliminarProdDet(" + cod + "," + id + ")' href='#'><i class='icon-trash bigger-130'></i></a>" +
                                "</div>" +
                                "</td></tr>");

                        calcMontoTotal(id);
                        $("#cod" + id).val("");
                        $("#canti" + id).val(0);
                        $("#subTotal" + id).val("0.00");
                    }
                }
            });
        } else {
            alert("Seleccione un producto.");
        }

    }

    function abrirProductos(id) {
        productos("<?php echo base_url("intranet/administrador/productos/2") ?>", id);
        $('#productos').dialog('open');
    }

    function abrirDetHab(id) {
        $(".submonto").remove();
        $.ajax({
            url: '<?php echo base_url("intranet/registro/detalleMontoRegistro"); ?>/' + id,
            contentType: "text/html; charset=UTF-8",
            dataType: "json",
            success: function(data) {
                $(data).each(function() {
                    if (this.registros !== null) {
                        $("#numMontos").val(this.registros.length);
                        for (var i = 0; i < this.registros.length; i++) {
                            newCajaTexto((i + 1), this.registros[i].submonto);
                        }
                    } else {
                        $("#numMontos").val(1);
                        newCajaTexto(1, 0);
                    }
                });
            }
        });
        $("#idTH").val(id);
        $('#detalleHabitacion').dialog('open');
    }

    function desplegar(id) {
        $("#cuerpo" + id).slideToggle();
    }

    function newCajaTexto(id, valor) {
        $("#subMontoAlq").append("<div class='control-group submonto'>" +
                "<label class='control-label' for='subMonto" + id + "'>Día " + id + "</label>" +
                "<div class='controls'>" +
                "<input type='text' id='subMonto" + id + "' value='" + valor + "' class='span9' onkeypress='return NumCheck(event,this)'>" +
                "</div>" +
                "</div>");
    }

    $(function() {
        $("#nuevoElem").click(function() {
            var numMontos = $("#numMontos").val();
            newCajaTexto((parseInt(numMontos) + 1), 0);
            $("#numMontos").val(parseInt(numMontos) + 1);
        });

        $("#totalizar").click(function() {
            var idTotHab = $("#idTH").val();
            var numMontos = $("#numMontos").val();
            var mtoTotal = 0;

            for (var i = 1; i <= numMontos; i++) {
                mtoTotal += parseFloat($("#subMonto" + i).val());
            }
            $("#totalHabit" + idTotHab).val(mtoTotal);
            calcMontoTotal(idTotHab);


            var columns = new Array();
            var z = 0;

            for (var i = 0; i < numMontos; i++) {
                columns[i] = {"registro": idTotHab === "" ? "0" : idTotHab, "submonto": parseFloat($("#subMonto" + (i + 1)).val())};
                z++;
            }

            var detalle = [];
            for (i in columns) {
                detalle[i] = columns[i];
            }
            var json = JSON.stringify(detalle);

            $.ajax({
                type: "POST",
                url: '<?php echo base_url("intranet/registro/registrarDetalleMonto"); ?>/' + idTotHab,
                data: {detalleMonto: json, monto: $("#montoTotal" + idTotHab).val()},
                //contentType: "application/json",
                dataType: "json",
                success: function(msg) {
                    if (msg !== true) {
                        alert("No se guardaron los cambios");
                    }
                }
            });

            $('#detalleHabitacion').dialog('close');
        });
    });

    function calcMontoTotal(id) {
        $("#montoTotal" + id).val(parseFloat($("#totalProd" + id).val() === "" ? 0 : $("#totalProd" + id).val()) + parseFloat($("#totalHabit" + id).val()));
    }

    function detalleRegistro(id, url) {
        $("#tabla" + id + " tbody tr").remove();
        $.ajax({
            url: url,
            contentType: "text/html; charset=UTF-8",
            dataType: "json",
            success: function(data) {
                var tabla = "";

                $(data).each(function() {
                    if (this.registros !== null) {
                        var total = 0;
                        for (var i = 0; i < this.registros.length; i++) {
                            var idDet = this.registros[i].id_det;
                            var codProd = this.registros[i].cod_prod;
                            var producto = this.registros[i].desc_prod;
                            var precio = this.registros[i].pre_prod;
                            var cantidad = this.registros[i].cantidad;
                            var subTotal = this.registros[i].sub_total;
                            var fecha = this.registros[i].fechaDet;
                            tabla += "<tr>" +
                                    "<td style='display: none;'>" + idDet + "</td>" +
                                    "<td style='display: none;'>" + codProd + "</td>" +
                                    "<td>" + producto + "</td>" +
                                    "<td>" + precio + "</td>" +
                                    "<td>" + cantidad + "</td>" +
                                    "<td>" + subTotal + "</td>" +
                                    "<td>" + fecha + "</td>" +
                                    "<td class='td-actions'>" +
                                    "<div class='hidden-phone visible-desktop action-buttons'>" +
                                    "<a class='red' onclick='eliminarProdDet(" + idDet + "," + id + ")' href='#'><i class='icon-trash bigger-130'></i></a>" +
                                    "</div>" +
                                    "</td>" +
                                    "</tr>";
                            total += parseFloat(subTotal);
                        }
                        $(".total").val(total);
                    }
                });
                $("#tabla" + id + " tbody").append(tabla);
                $(data).each(function() {
                    if (this.monto !== null) {
                        $("#totalHabit" + id).val(this.monto[0].submonto);
                        $("#montoTotal" + id).val(this.monto[0].monto);
                    }
                });
            }
        });
    }

    function eliminarProdDet(cod, id) {
        var eliminar = confirm("De verdad desea eliminar este registro?");
        if (eliminar) {
            $.ajax({
                type: "GET",
                url: '<?php echo base_url("intranet/reserva/eliminarProdDet") ?>/' + cod,
                contentType: "text/html; charset=UTF-8",
                dataType: "json",
                success: function(msg) {
                    if (msg === true) {
                        detalleRegistro(id, "<?php echo base_url('intranet/reserva/detalleRegistro') ?>/" + id);
                    }
                }
            });
        }
    }

    function ventRegistroInf(reg) {
        ventana("infoRegistro", 520, 540, true, "center");
        registroInfo('<?php echo base_url('intranet/registro/infoRegistro') ?>/' + reg);
        $('#infoRegistro').dialog('open');
    }

</script>


